var dayOptions = Array();
$(function() {
	var tryAgain = $("#try-again").val();
	for(var i = 28 ; i<=31;i++) {
		dayOptions[i]=$("#dayBirthDateRegister [value="+i+"]");
	}
	$("#login-link").find("a").click(function(){
		login();
		return false;
	});
	
	$("#login-guest").find("a").click(function(){
		alert('not supported...');
		return false;
	});
	$("#login-register").find("a").click(function(){
		showRegisterForm();
		//alert('The registration has been disabled...');
		return false;
	});
	$("#mail").keyup(function(event){
		setFocusNext(event,'loginForm #password');
	});
	$("#password").keyup(function(event){
		loginOnEnter(event);
	});
	
	$("#monthBirthDateRegister,#yearBirthDateRegister").change(function(){
		manageDays();
	});
	
	$("#register-previous").click(function(){
		showLoginForm();
		return false;
	});
	
	$("#register-next").click(function(){
		register();
		return false;
	});
	
	$("#registerForm").validate({
			focusCleanup: true,
			errorElement:'span',
			//errorPlacement: function(error, element) {},
			rules: {
				mail: {
					required:true,
					email:true
				},
				firstName:'required',
				password: {
					required:true,
					minlength:6,
					maxlength:25
				},
				passwordConfirm: {
					equalTo:"#passwordRegister",
					required:true
				},
				dayBirthDate: {
					required:true
				},
				monthBirthDate: {
					required:true
				},
				yearBirthDate: {
					required:true
				}
			},
			messages : {
				mail: {
					required:tryAgain,
					email:tryAgain
				},
				firstName:tryAgain,
				password: {
					required:tryAgain,
					minlength:tryAgain,
					maxlength:tryAgain
				},
				passwordConfirm: {
					equalTo:tryAgain,
					required:tryAgain
				},
				dayBirthDate: {
					required:tryAgain
				},
				monthBirthDate: {
					required:tryAgain
				},
				yearBirthDate: {
					required:tryAgain
				}
				
			},
			errorPlacement: function(error, element) {
				$("#registerForm label[for='"+element.attr('name')+"']").attr("class","error-label");
				if (element.attr('id')=="yearBirthDateRegister" || element.attr('id')=="dayBirthDateRegister" || element.attr('id')=="monthBirthDateRegister") {
					$("#registerForm label[for='birthDate']").attr("class","error-label");
					if ($("#registerForm .error[for='yearBirthDateRegister']").length==0 && 
							$("#registerForm .error[for='monthBirthDateRegister']").length==0 &&
							$("#registerForm .error[for='dayBirthDateRegister']").length==0) {
						error.css('margin-left','3px');
						error.insertAfter($("#yearBirthDateRegister"));
					}
				} else {
					if ($("#registerForm label[for='"+element.attr('name')+"']").length!=2) {
						error.insertAfter(element);
					}
				}
			}
		});
	
});

$(window).load(function(){
	/**
	 * This permit to animate the login screen after the load of the background images
	 */
	var image = preloadBackgroundImage($(".form-top").css('background-image'));
	image.onload = function(){
		var image2 = preloadBackgroundImage($(".form-bloc").css('background-image'));
		image2.onload = function(){
			var image3 = preloadBackgroundImage($(".bloc-bottom").css('background-image'));
			image3.onload = function(){
				setTimeout('$("#login-bloc").fadeIn();',150);
			}
		}
	}
});
/**
 * This function hide the login form and shows
 * the registration form.
 */
function showRegisterForm() {
	$("#login-part").slideUp();
	$("#register-part").slideDown();
}

/**
 * This function hide the register form and shows
 * the login form.
 */
function showLoginForm() {
	$("#login-part").slideDown();
	$("#register-part").slideUp();
	cleanRegisterForm();
}

/**
 * This function clean the register form from its errors
 */
function cleanRegisterForm() {
	$("span.error").each(function(){//Check
		$(this).remove();
	});
	$(".error-label").each(function(){//Check
		$(this).removeClass("error-label");
	});
	
}

/**
 * Log the user in using the loginForm form
 */
function login() {
	$(".button ").hide();
	$(".progress_bar-login").fadeIn();
	
	submitFormAjax(computeAction('loginForm'),computeData('loginForm'),'POST',function(data){
		if (getMessage("Error",data).length==0) {
			$("#login-bloc").fadeOut();
			document.location.href=getMessage("URL",data);
		} else {
			$(".progress_bar-login ").hide();
			$(".button").show();
			$(".full-bloc").effect('shake',40);
		}
	  });
}

/**
 * Register the user using the loginform form
 */
function register() {
	cleanRegisterForm();
	if ($("#registerForm").valid()) {
		$(".arrow ").hide();
		$(".progress_bar-register").fadeIn();
		submitFormAjax(computeAction('registerForm'),computeData('registerForm'),'POST',function(data){
			var errorMessage = getMessage("Error",data);
			if (errorMessage.length==0) {
				$("#register-next").unbind('click');
				$("#register-next").click(function(){
					loadHome();
					return false;
				});
				
				$(".previous").addClass('previous-notenable');
				$(".previous-notenable").removeClass('previous');
				$("#register-previous").unbind('click');
				$("#register-previous").click(function(){return false;});
				$(".arrow ").show();		
				$(".progress_bar-register").hide();
				$("#table-register-form").slideUp();
				$(".register-done").slideDown();
			} else if (errorMessage=="error-mail"){
				addError('mail');
				$(".arrow ").show();
				$(".progress_bar-register").hide();
			} else {
				$(".arrow ").show();
				$(".progress_bar-register").hide();
				alert('registration refused...');
			}
		  });
	}
}

function loadHome() {
	document.location.href='/home';
}

function loginOnEnter(event) {
	if (isReturn(event)) {
		login();
	}
}

/**
 * This function manages the number of days to display depending on the month and the year
 * @param month
 * @param day
 * @param year
 */
function manageDays() {
	if ($("#monthBirthDateRegister").val()!="") {
		var year = $("#yearBirthDateRegister").val();
		var numberOfDays = daysInMonth($("#monthBirthDateRegister").val(),year!=""?year:2011);
		for (var i = numberOfDays+1; i <=31;i++) {
			$("#dayBirthDateRegister [value="+i+"]").remove();
		}
		for(var i = 28 ; i<numberOfDays+1;i++) {
			if ($("#dayBirthDateRegister [value="+i+"]").length==0) {
				$("#dayBirthDateRegister").append(dayOptions[i]);
			}
		}
	} else {
		for(var i = 28 ; i<=31;i++) {
			if ($("#dayBirthDateRegister [value="+i+"]").length==0) {
				$("#dayBirthDateRegister").append(dayOptions[i]);
			}
		}
	}
}

/**
 * This function add an error to an element
 * @param name
 */
function addError(name) {
	$("#registerForm label[for='"+name+"']").attr("class","error-label");
	
	$('<span for="'+name+'" generated="true" class="error">&lt; Try again</span>').insertAfter($("#registerForm [name='"+name+"']"));
}